<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/Snoopy.class.php');
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom_utf8.php');
error_reporting(0);
//每10分钟获取尚未开始的比赛的必发指数
class GetGameInfosOkoooSnoopyCommand extends CConsoleCommand  
{
    public function run($args) {
        //使用snoopy得到html
        $snoopy = new Snoopy;
        $i=0;
        $sql="select * from tbl_proxy where writetime>date(DATE_ADD(now(),INTERVAL -1 MONTH)) ";
        $ipList=Yii::app()->db->createCommand($sql)->queryAll();
        $sql = "select * from tbl_gameinfoOkooo where gametime>now()";
        $gameArry=Yii::app()->db->createCommand($sql)->queryAll();
        if(!isset($gameArry)){
            return;
        }
        while($i<100){
            $_index = rand(0,count($ipList)-1);
            $snoopy->proxy_host = $ipList[$_index]['ipaddress'];
            $snoopy->proxy_port = $ipList[$_index]['port'];
            $snoopy->agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
            $snoopy->referer = "http://www.okooo.com/jingcai/";
            $snoopy->cookies["FirstOKURL"]="http%3A//www.okooo.com/";
            $snoopy->cookies["First_Source"]="www.okooo.com";
            printf("[ipaddress=%s] [port=%s]\n", $ipList[$_index]['ipaddress'], $ipList[$_index]['port']);
            $url="http://www.okooo.com/soccer/match/611687/exchanges/";
            printf("开始测试代理服务器[%s]\n", $i);
            try{
                if($snoopy->fetch($url)) {
                    printf("服务器可用\n");
                    break;
                }
            }
            catch(Exception $e){
                $proxyObj=Proxy::model()->find(
                            array(
                                'condition'=>'ipaddress=:ipaddress and port=:port',
                                'params'=>array(
                                            ':ipaddress'=>$ipList[$_index]['ipaddress'],
                                            ':port'=>$ipList[$_index]['port'],
                                            )
                                )
                            );
                $proxyObj->status=1;
                $proxyObj->update();
                printf("update status to 1![ip={%s}] [port=%s]\n", $ipList[$_index]['ipaddress'], $ipList[$_index]['port']);
                $sql="select * from tbl_proxy where status=0";
                $ipList=Yii::app()->db->createCommand($sql)->queryAll();
            }
            $i++;
        }
        if($i>=100){
            printf("无服务器可用！\n");
            return;
        }
        foreach ($gameArry as $key => $value) {
            $gameid=$value["gameid"];
            $gameIndex=$value["gameindex"];
            printf("[issue=%s] [gameindex=%s] [gametime=%s] [gametype=%s] [hometeam=%s] [awayteam=%s]\n", $value["issue"], $value["gameindex"], $value["gametime"], $value["gametype"], $value["hometeam"], $value["awayteam"]);
            $bfurl=sprintf("http://www.okooo.com/soccer/match/%s/exchanges/", $gameid);//必发数据地址
            printf("必发数据地址：%s\n", $bfurl);
            try{
                if(!$snoopy->fetch($bfurl)) { 
                    continue;
                }
            }
            catch(Exception $e){
                if(!$snoopy->fetch($bfurl)) { 
                    continue;
                }
            }

            $htmlBf = str_get_html($snoopy->results);
            if(!isset($htmlBf) || !is_object($htmlBf)){
                continue;
            }

            $table = $htmlBf->find("div[class=bfTable] table", 0);
            if(!isset($table)){
                $htmlBf->clear();
                unset($htmlBf);
                continue;
            }
            $tr=$table->find("tr", 2);
            $bfprice3=$tr->find("td", 5)->plaintext;//胜挂牌价
            $bfexchange3=$tr->find("td", 6)->plaintext;//胜成交量

            $tr=$table->find("tr", 3);
            $bfprice1=$tr->find("td", 5)->plaintext;//平挂牌价
            $bfexchange1=$tr->find("td", 6)->plaintext;//负挂牌价
            
            $tr=$table->find("tr", 4);
            $bfprice0=$tr->find("td", 5)->plaintext;//平成交量
            $bfexchange0=$tr->find("td", 6)->plaintext;//负成交量
            $htmlBf->clear();
            unset($htmlBf);
            printf("[bfprice3=%s] [bfexchange3=%s] [bfprice1=%s] [bfexchange1=%s] [bfprice0=%s] [bfexchange0=%s]\n", $bfprice3, $bfexchange3, $bfprice1, $bfexchange1, $bfprice0, $bfexchange0);
            $gameInfoOkoooObj=GameInfoOkooo::model()->find(array('condition'=>'gameid=:gameid','params'=>array(':gameid'=>$gameid)));
            if (isset($gameInfoOkoooObj)){
                $gameInfoOkoooObj->bfprice3=$bfprice3;
                $gameInfoOkoooObj->bfprice1=$bfprice1;
                $gameInfoOkoooObj->bfprice0=$bfprice0;
                $gameInfoOkoooObj->bfexchange3=$bfexchange3;
                $gameInfoOkoooObj->bfexchange1=$bfexchange1;
                $gameInfoOkoooObj->bfexchange0=$bfexchange0;
                $gameInfoOkoooObj->updatetime=date("Y-m-d H:i:s");
                if ($gameInfoOkoooObj->update()) {
                    printf("update [期号：%s] [序号：%s] [bfprice3=%s] [bfexchange3=%s] [bfprice1=%s] [bfexchange1=%s] [bfprice0=%s] [bfexchange0=%s]\n", $value["issue"], $gameIndex, $bfprice3, $bfexchange3, $bfprice1, $bfexchange1, $bfprice0, $bfexchange0);
                }
            }

            //插入必发明细
            $bfoddsinfo=new BfOddsInfo;
            $bfoddsinfo->gameid=$gameid;
            $bfoddsinfo->bfprice3=$bfprice3;
            $bfoddsinfo->bfprice1=$bfprice1;
            $bfoddsinfo->bfprice0=$bfprice0;
            $bfoddsinfo->bfexchange3=$bfexchange3;
            $bfoddsinfo->bfexchange1=$bfexchange1;
            $bfoddsinfo->bfexchange0=$bfexchange0;
            //计算盈亏
            $bfyingkui3=($bfexchange3+$bfexchange1+$bfexchange0)-$bfprice3*$bfexchange3;
            $bfyingkui1=($bfexchange3+$bfexchange1+$bfexchange0)-$bfprice1*$bfexchange1;
            $bfyingkui0=($bfexchange3+$bfexchange1+$bfexchange0)-$bfprice0*$bfexchange0;

            $bfoddsinfo->yk3=$bfyingkui3;
            $bfoddsinfo->yk1=$bfyingkui1;
            $bfoddsinfo->yk0=$bfyingkui0;
            $bfoddsinfo->writetime=date("Y-m-d H:i:s");
            $bfoddsinfo->save();
        }
    }
}